package com.acce.product.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class Utils {
	
	private static Logger logger = Logger.getLogger(Utils.class);

	public static String getCurrTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());
	}

	public static String getCurrTimeMillions() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return df.format(new Date());
	}

	public static String getSerialNumber() {
		SimpleDateFormat df = new SimpleDateFormat("yySSSddHHss");
		return df.format(new Date());
	}
	
	public static String timeDifs(long d1, long d2){
		long mss = d2 - d1;
		long days = mss / (1000 * 60 * 60 * 24);  
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);  
	    long seconds = (mss % (1000 * 60)) / 1000;  
	    return days + "-" + hours + "-" + minutes + "-"  
		            + seconds;
	}
	
	public static String makeAtUserId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("KL");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String makeCompanyId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("CP");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String makeDepartmentId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("DP");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String makePasswordId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("PW");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String makeLoginId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("LG");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String makeOperId(){
		SimpleDateFormat df = new SimpleDateFormat("yyssSSS");
		StringBuffer fm = new StringBuffer(); 
		fm.append("OP");
		fm.append(df.format(new Date()));
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			fm.append(r.nextInt(9));
		}
		return fm.toString();
	}
	
	public static String getUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replaceAll("-", "");
	}
	
	public final static String getIpAddress(HttpServletRequest request) throws IOException {
		String ip = request.getHeader("X-Forwarded-For");
		if (logger.isInfoEnabled()) {
			logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
				if (logger.isInfoEnabled()) {
					logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
				if (logger.isInfoEnabled()) {
					logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
				if (logger.isInfoEnabled()) {
					logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
				if (logger.isInfoEnabled()) {
					logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
				if (logger.isInfoEnabled()) {
					logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
				}
			}
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; index++) {
				String strIp = (String) ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip;
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(makeAtUserId());
	}
	
	public static String makeProductId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("PD");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makePurchaseId(){
		StringBuffer fm = new StringBuffer(); 
//		fm.append("PH");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(9));
		return fm.toString();
	}
	
	public static String makePurchaseListId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("PL");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makeInquiryId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("IH");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makeInquiryListId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("IL");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makeQuoteId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("QH");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makeQuoteListId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("QL");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
	
	public static String makeChannelId(){
		StringBuffer fm = new StringBuffer(); 
		fm.append("CR");
		fm.append(getSerialNumber());
		Random r = new Random();
		fm.append(r.nextInt(999));
		return fm.toString();
	}
}
